package com.shd.edanpei.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.TextView;

import com.shd.edanpei.R;
import com.shd.edanpei.base.BaseActivity;
import com.shd.edanpei.common.Constant;
import com.shd.edanpei.base.dialog.DialogUtil;
import com.shd.edanpei.base.network.ApiParams;
import com.shd.edanpei.bean.UserBean;
import com.shd.edanpei.db.DaoManager;
import com.shd.edanpei.enums.GenderEnum;
import com.shd.edanpei.enums.RoleEnum;
import com.shd.edanpei.image.LoadImageCtrl;
import com.shd.edanpei.presenter.contract.UserInfoContract;
import com.shd.edanpei.presenter.UserInfoPresenter;

import butterknife.BindView;
import butterknife.OnClick;
import de.hdodenhof.circleimageview.CircleImageView;


public class UserInfoActivity extends BaseActivity<UserInfoPresenter>
        implements UserInfoContract.UserInfoIView {

    private static final int PHOTO = 101;
    private static final int NICK_NAME = 102;

    @BindView(R.id.headline)
    TextView headline;
    @BindView(R.id.iv_head)
    CircleImageView ivHead;
    @BindView(R.id.tv_phone)
    TextView tvPhone;
    //    @BindView(R.id.tv_nickName)
//    TextView tvNickName;
    @BindView(R.id.tv_gender)
    TextView tvGender;
    @BindView(R.id.tv_birthday)
    TextView tvBirthday;

    @BindView(R.id.root1)
    View root1;
    @BindView(R.id.root2)
    View root2;
    @BindView(R.id.tv_account)
    TextView tvAccount;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.ll_changePassword)
    View password;
    private int roleType;

    @Override
    protected int setLayout() {
        return R.layout.activity_userinfo;
    }

    @Override
    protected UserInfoPresenter bindPresenter() {
        return new UserInfoPresenter(this);
    }

    @Override
    protected void initView(@Nullable Bundle savedInstanceState) {
        loadImageCtrl = new LoadImageCtrl(this);
        headline.setText("个人信息");
        roleType = DaoManager.getInstance().getUserBean().getRoleType();

        if (roleType == RoleEnum.user.getId()) {
            root2.setVisibility(View.VISIBLE);
            password.setVisibility(View.GONE);
        } else {
            if (roleType == RoleEnum.other.getId()
                    || roleType == RoleEnum.boss.getId()) {
                findViewById(R.id.ll_companyInfo).setVisibility(View.GONE);
            }
            root1.setVisibility(View.VISIBLE);
            password.setVisibility(View.VISIBLE);
        }


    }

    @Override
    protected void onStart() {
        super.onStart();
        UserBean userBean = DaoManager.getInstance().getUserBean();
        if (!TextUtils.isEmpty(userBean.getAvatarUrl())) {
            loadImageCtrl.getGlide().load(userBean.getAvatarUrl()).into(ivHead);
        }
        tvPhone.setText(userBean.getMobile());
//        tvNickName.setText(userBean.getNickName());
        tvGender.setText(userBean.getGender());
        tvBirthday.setText(userBean.getBirthday());
        tvName.setText(userBean.getRealName());

        if (roleType == RoleEnum.user.getId()) {
            tvAccount.setText(userBean.getAccountNo());

        } else {
            tvAccount.setText(userBean.getMobile());

        }
    }

    @Override
    protected void initListener() {

    }

    @OnClick(R.id.iv_headline_back)
    void close() {
//        finish();
        onBackPressed();
    }

    @OnClick(R.id.ll_head)
    void clickHead() {
        ChoosePicActivity.start(this, PHOTO);
    }

    @OnClick(R.id.ll_phone)
    void clickPhone() {
        startActivity(PhoneActivity.class);
    }
//
//    @OnClick(R.id.ll_nickName)
//    void clickNickName() {
//        EditActivity.start(this, NICK_NAME, "修改昵称", tvNickName.getText().toString());
//    }

    @OnClick(R.id.ll_realName)
    void onRealName() {
        Bundle bundle = new Bundle();
        bundle.putInt(Constant.FROM, CertificationActivity.FORM_DATA);
        startActivity(CertificationActivity.class, bundle);
    }

    @OnClick(R.id.ll_companyInfo)
    void onCompanyInfo() {
        startActivity(CompanyInfoActivity.class);
    }

    @OnClick(R.id.ll_gender)
    void clickGender() {
        DialogUtil.listDialog(this, "性别", R.array.gender, new DialogUtil.Listener() {
            @Override
            public void done(String data) {
                tvGender.setText(data);
                mPresenter.update(ApiParams.GENDER, data.equals(GenderEnum.male.getValue()) ? GenderEnum.male.getKey() : GenderEnum.female.getKey());
            }
        });
    }

    @OnClick(R.id.ll_birthday)
    void clickBirthday() {
        DialogUtil.dateDialog(this, "出生日期", new DialogUtil.Listener() {
            @Override
            public void done(String data) {
                tvBirthday.setText(data);
                mPresenter.update(ApiParams.BIRTHDAY, data);
            }
        });
    }

    @OnClick(R.id.ll_qrCode)
    void clickQrCode() {
        startActivity(QrcodeActivity.class);
    }

    @OnClick(R.id.ll_changePassword)
    void clickChangePassword() {
        //修改密码
        Bundle bundle2 = new Bundle();
        bundle2.putInt(Constant.TYPE, ResetLoginPwdActivity.UPDATA_PWD_LAYOUT);
        startActivity(ResetLoginPwdActivity.class, bundle2);
    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        //编辑返回
        if (requestCode == ChoosePicActivity.INTENT_REQUEST_CODE) {
            if (null != data) {
                switch (resultCode) {
                    case PHOTO:
                        String picturePath = data.getStringExtra(ChoosePicActivity.INTENT_BACK_DATA);
                        loadImageCtrl.getGlide().load(picturePath).into(ivHead);
                        mPresenter.uploadPicture(picturePath);
                        break;
                }
            }
        }
//        //返回
//        if (requestCode == EditActivity.INTENT_REQUEST_CODE) {
//            if (null != data) {
//                switch (resultCode) {
//                    case NICK_NAME:
//                        setTextData(R.id.tv_user_nickname, data.getStringExtra(EditActivity.INTENT_BACK_DATA));
////                        mPresenter.update(Api.Parameter.nickName, data.getStringExtra(EditActivity.INTENT_BACK_DATA));
//                        break;
//                    default:
//                        break;
//
//                }
//            }
//        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            onBackPressed();
        }
        return false;
    }
}
